/*!
    \copyright  Copyright (c) 2020 - 2025 Qualcomm Technologies International, Ltd.\n
                All Rights Reserved.\n
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file       usb_audio_anc_helpers.c
    \ingroup    usb_audio
    \brief      Collection of helper functions for ANC tuning
*/

#include "usb_audio_fd.h"

#ifndef MESSAGE_USB_EXT_ALT_INTERFACE
/* This code won't work on CDA2 architecture */

bool UsbAudio_GetInterfaceInfoFromDeviceType(usb_audio_device_type_t intf_type, usb_audio_interface_info_t * interface_info)
{
    usb_audio_info_t *usb_audio = USB_AUDIO_GET_DATA();

    PanicNull(interface_info);

    while (usb_audio)
    {
        usb_audio_streaming_info_t  *streaming_info = UsbAudio_GetStreamingInfo(usb_audio, intf_type);

        if(streaming_info)
        {
            interface_info->is_to_host = (streaming_info->ep_address & end_point_to_host) ? 1 : 0;
            if(interface_info->is_to_host)
            {
                interface_info->streamu.mic_sink = StreamUsbEndPointSink(streaming_info->ep_address);
            }
            else
            {
                interface_info->streamu.spkr_src = StreamUsbEndPointSource(streaming_info->ep_address);
            }

            interface_info->sampling_rate = streaming_info->current_sampling_rate;
            interface_info->volume_db = streaming_info->volume_status.volume_db;
            interface_info->mute_status = streaming_info->volume_status.mute_status;
            interface_info->channels = streaming_info->channels;
            interface_info->frame_size = streaming_info->frame_size;

            return TRUE;
        }

        usb_audio = usb_audio->next;
    }

    return FALSE;
}

bool UsbAudio_SetAudioChainBusy(Source source)
{
    DEBUG_LOG_VERBOSE("UsbAudio_SetAudioChainBusy");
    usb_audio_info_t *usb_audio = USB_AUDIO_GET_DATA();

    while (usb_audio)
    {
        if(!usb_audio->is_pending_delete)
        {
            if(usb_audio->headphone != NULL && usb_audio->headphone->spkr_src == source)
            {
                usb_audio->headphone->chain_active = TRUE;
                return TRUE;
            }
            if(usb_audio->headset != NULL && usb_audio->headset->spkr_src == source)
            {
                usb_audio->headset->chain_active = TRUE;
                return TRUE;
            }
        }
        usb_audio = usb_audio->next;
    }
    return FALSE;
}

void UsbAudio_ClearAudioChainBusy(Source source)
{
    DEBUG_LOG_VERBOSE("UsbAudio_ClearAudioChainBusy");
    usb_audio_info_t *usb_audio = USB_AUDIO_GET_DATA();
    while (usb_audio)
    {
        if(usb_audio->headphone != NULL && usb_audio->headphone->spkr_src == source)
        {
            usb_audio->headphone->chain_active = FALSE;
            break;
        }
        if(usb_audio->headset != NULL && usb_audio->headset->spkr_src == source)
        {
            usb_audio->headset->chain_active = FALSE;
            break;
        }
        usb_audio = usb_audio->next;
    }

    if (usb_audio && usb_audio->is_pending_delete)
    {
        UsbAudio_TryFreeData(usb_audio);
    }
}

#endif /* !MESSAGE_USB_EXT_ALT_INTERFACE */
